package com.tour.tour.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tour.tour.common.Response;
import com.tour.tour.dto.CommentDTO;
import com.tour.tour.entity.PostInfo;
import com.tour.tour.exception.UserStatusException;
import com.tour.tour.mapper.PostMapper;
import com.tour.tour.mapper.UserInfoMapper;
import com.tour.tour.mapper.UserMapper;
import com.tour.tour.service.PostService;
import com.tour.tour.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 76004
 * @apiNote
 * @date 2023/4/25 8:32
 */
@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Response<Void> addPost(PostInfo postInfo, HttpServletRequest httpServletRequest) throws UserStatusException {
        String phone = UserInfoUtil.getUid(httpServletRequest);
        postInfo.setCreateTime(new Date());
        postInfo.setUid(phone);
        postMapper.insert(postInfo);
        return Response.success();
    }

    @Override
    public Response<List<PostInfo>> selectPostInfoPage(Integer pageSize, Integer pageNum) {
        List<PostInfo>postInfos = postMapper.selectPage(new Page<PostInfo>(pageNum, pageSize),
                new QueryWrapper<PostInfo>().eq("level", 1)).getRecords();
        for(PostInfo postInfo: postInfos){
            postInfo.setUrl(userMapper.selectById(postInfo.getUid()).getAvatar());
            postInfo.setComments(postMapper.selectList(
                    new QueryWrapper<PostInfo>().eq("level",2)
                            .eq("parent_id",postInfo.getId())).stream().map(comment->{
                                comment.setUrl(userMapper.selectById(comment.getUid()).getAvatar());
                                return comment;
            }).collect(Collectors.toList()));
        }

        return Response.success(postInfos);
    }

    @Override
    public Response<List<PostInfo>> selectPostInfoByUsername(String username, Integer pageSize, Integer pageNum) {
        List<PostInfo> postInfos = postMapper.selectPage(new Page<PostInfo>(pageNum, pageSize),
                new QueryWrapper<PostInfo>().eq("username",username)).getRecords();
        return Response.success(postInfos);
    }

    @Override
    public Response<PostInfo> selectPost(Long id) {
        PostInfo postInfo = postMapper.selectById(id);
        return Response.success(postInfo);
    }

    @Override
    public Response<Void> deletePost(Long id) {
        postMapper.deleteById(id);
        return Response.success();
    }

    @Override
    public Response<Void> updatePost(PostInfo postInfo) {
        postMapper.updateById(postInfo);
        return Response.success();
    }

    public Response<Void> comment(CommentDTO commentDTO, HttpServletRequest httpServletRequest) throws UserStatusException {
        String uid = UserInfoUtil.getUid(httpServletRequest);
        PostInfo postInfo = postMapper.selectById(commentDTO.getParentId());
        if(postInfo == null){
            throw new RuntimeException("该评论不存在");
        }
        PostInfo comment = BeanUtil.copyProperties(commentDTO, PostInfo.class);
        comment.setUid(uid);
        comment.setStatus(1);
        comment.setLevel(2);
        comment.setCreateTime(new Date());
        postMapper.insert(comment);
        return Response.success();
    }
}
